package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.mapper.ExpenditureMapper;
import com.qf.pojo.Expenditure;
import com.qf.service.ExpenditureService;
import com.qf.utils.Constant;
import com.qf.utils.ResultMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class ExpenditureServiceImpl extends ServiceImpl<ExpenditureMapper, Expenditure> implements ExpenditureService {
    @Autowired
    ExpenditureMapper expenditureMapper;

    @Override
    public ResultMsg findAll(Integer current, Integer pageSize) {
        current = current <= 0 ? 1 : current;
        pageSize = pageSize <= 0 ? 20 : pageSize;
        Page<Expenditure> page = new Page<>(current, pageSize);
        QueryWrapper<Expenditure> wrapper = new QueryWrapper<>();
        wrapper.eq("expenditure_delete", Constant.NO_DELETE);
        Page<Expenditure> all = expenditureMapper.findAll(page, wrapper);
        return ResultMsg.SUCCESS(all);
    }

    @Override
    public ResultMsg findOne(Integer expenditureId) {
        QueryWrapper<Expenditure> wrapper = new QueryWrapper<>();
        wrapper.eq("expenditure_id", expenditureId);
        wrapper.eq("expenditure_delete", Constant.NO_DELETE);
        Expenditure one = this.getOne(wrapper);
        if (one != null)
            return ResultMsg.SUCCESS(one);
        return ResultMsg.FAILED(Constant.LZY + 1, "没有找到该条支出信息");
    }

    @Override
    public ResultMsg findAllByCondition(String startTime, String endTime, String expenditureFunctionary, String expenditureType, String paymentMethod, Integer current, Integer pageSize) {
        current = current <= 0 ? 1 : current;
        pageSize = pageSize <= 0 ? 20 : pageSize;
        QueryWrapper<Expenditure> wrapper = new QueryWrapper<>();
        wrapper.eq("expenditure_delete", Constant.NO_DELETE);
        if (!(startTime.equals("") || endTime.equals(""))) {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            try {
                Date startTimeDate = format.parse(startTime);
                Date endTimeDate = format.parse(endTime);
                wrapper.between("expenditure_date", new Timestamp(startTimeDate.getTime()), new Timestamp(endTimeDate.getTime()));
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
        }
        if (!expenditureFunctionary.equals(""))
            wrapper.like("expenditure_functionary", expenditureFunctionary);
        if (!expenditureType.equals(""))
            wrapper.like("expenditure_type", expenditureType);
        if (!paymentMethod.equals(""))
            wrapper.like("payment_method", paymentMethod);
        Page<Expenditure> page = new Page<>(current, pageSize);
        Page<Expenditure> all = expenditureMapper.findAll(page, wrapper);
        return ResultMsg.SUCCESS(all);
    }

    @Override
    @Transactional
    public ResultMsg addExpenditure(Expenditure expenditure) {
        expenditure.setExpenditureId(null);
        expenditure.setExpenditureDelete(Constant.NO_DELETE);
        boolean save = this.save(expenditure);
        if (save)
            return ResultMsg.SUCCESS(expenditure);
        return ResultMsg.FAILED(Constant.LZY + 1, "插入失败");
    }

    @Override
    @Transactional
    public ResultMsg modifyExpenditure(Expenditure expenditure) {
        if (expenditure.getExpenditureId() == null)
            return ResultMsg.FAILED(Constant.LZY + 2, "expenditureId 不能为 null");
        boolean b = this.updateById(expenditure);
        if (b)
            return ResultMsg.SUCCESS(expenditure);
        return ResultMsg.FAILED(Constant.LZY + 1, "修改失败,未改变任何内容");
    }

    @Override
    @Transactional
    public ResultMsg deleteExpenditure(Integer expenditureId) {
        if (expenditureId == null)
            return ResultMsg.FAILED(Constant.LZY + 2, "expenditureId 不能为 null");
        QueryWrapper<Expenditure> wrapper = new QueryWrapper<>();
        wrapper.eq("expenditure_id", expenditureId);
        boolean remove = this.remove(wrapper);
        if (remove)
            return ResultMsg.SUCCESS();
        return ResultMsg.FAILED(Constant.LZY + 1, "删除失败");
    }
}
